export class AppError extends Error {
  constructor(
    public status: number,
    public override message: string,
    public code: string
  ) {
    super(message);
    this.name = "AppError";
  }
}

// 常用错误状态
export const HTTP_STATUS = {
  BAD_REQUEST: 400,
  UNAUTHORIZED: 401,
  FORBIDDEN: 403,
  NOT_FOUND: 404,
  INTERNAL_SERVER_ERROR: 500,
} as const;

// 错误代码
export const ERROR_CODES = {
  AUTH: {
    INVALID_CREDENTIALS: "AUTH.INVALID_CREDENTIALS",
    TOKEN_EXPIRED: "AUTH.TOKEN_EXPIRED",
    INVALID_TOKEN: "AUTH.INVALID_TOKEN",
    UNAUTHORIZED: "AUTH.UNAUTHORIZED"
  }
} as const;

// 认证相关错误
export const AuthErrors = {
  InvalidCredentials: () => 
    createError(HTTP_STATUS.UNAUTHORIZED, "用户名或密码错误", ERROR_CODES.AUTH.INVALID_CREDENTIALS),
  TokenExpired: () => 
    createError(HTTP_STATUS.UNAUTHORIZED, "Token已过期", ERROR_CODES.AUTH.TOKEN_EXPIRED),
  InvalidToken: () => 
    createError(HTTP_STATUS.UNAUTHORIZED, "无效的token", ERROR_CODES.AUTH.INVALID_TOKEN),
  Unauthorized: () => 
    createError(HTTP_STATUS.UNAUTHORIZED, "未授权访问", ERROR_CODES.AUTH.UNAUTHORIZED)
};

// 创建错误实例的辅助函数
export function createError(status: number, message: string, code: string) {
  return new AppError(status, message, code);
} 